package cn.com.darly.videoandroidpm.common;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

/** 加密和解密過程
 * @author Darly/张宇辉/2018/4/25 9:41
 * @version 1.0/cn.com.darly.videoandroidpm.common
 */
public class EncryperAES {
    /**
     * 加密
     * @param ar
     * @return
     */
    public static String Encode(String ar){
        String t = null;
        if (ar!=null&&!"".equals(ar)){
            StringBuffer buffer = new StringBuffer(ar);
            buffer.append("DarlyZhangyuhui");
            char[] s =  buffer.toString().toCharArray();
            for (int i = 0;i<s.length;i++){
                s[i]= (char) (~(s[i]^1003));
            }
            t = new String(s);
        }
        return t;
    }

    /**
     * 解码
     * @param ar
     * @return
     */
    public static String Dncode(String ar){
        String t = null;
        if (ar!=null&&!"".equals(ar)){
            char[] s =  ar.toCharArray();
            for (int i = 0;i<s.length;i++){
                s[i]= (char) (~s[i]^1003);
            }
            t = new String(s);
        }
        return t.split("DarlyZhangyuhui")[0];
    }

    /**
     * 判断是否是正常手机号
     * @param str
     * @return
     * @throws PatternSyntaxException
     */
    public static boolean isChinaPhoneLegal(String str)
            throws PatternSyntaxException {
        String regExp = "^(1)\\d{10}$";
        Pattern p = Pattern.compile(regExp);
        Matcher m = p.matcher(str);
        return m.matches();
    }
}
